import axios from "../api/request";
import MockAdapter from "axios-mock-adapter";
import {studentList, studentMenu, teacherList, teacherMenu} from "@/mock_database/index";

//创建默认实例对象
let Mock = new MockAdapter(axios);

//模拟服务器响应
Mock.onPost("/api/login").reply((config: any) => {
    //获取请求的参数
    const user = JSON.parse(config.data);
    let msg = "";
    //模拟账户校验
    for(let i = 0; i < studentList.length; i++){
        if (studentList[i].account === user.account && studentList[i].password === user.password) {
            msg = "登录成功！";
            if (user.account === user.password) {
                msg = "登录成功！注意：账号密码一样！请及时修改！";
            }
            return [
                200,
                {
                    code: 1,
                    msg: msg,
                    //模拟token
                    token: "12345678910",
                    userInfo: studentList[i],
                    menu: studentMenu,
                    userType: 'student'
                },
            ];
        }
    }
    for(let i = 0; i < studentList.length; i++){
        if (teacherList[i].account === user.account && teacherList[i].password === user.password) {
            msg = "登录成功！";
            if (user.account === user.password) {
                msg = "登录成功！注意：账号密码一样！请及时修改！";
            }
            return [
                200,
                {
                    code: 1,
                    msg: msg,
                    //模拟token
                    token: "12345678910",
                    userInfo: teacherList[i],
                    menu: teacherMenu,
                    userType: 'teacher'
                },
            ];
        }
    }
    msg = "登陆失败, 账号或者密码错误！";
    return [
        200,
        {
            code: 0,
            msg: msg,
        },
    ];
});

Mock.onGet("/api/getTeacherList").reply((config: any) => {
    return [
        200,
        {
            code: 1,
            msg: "查询教师列表成功！",
            teacherList: teacherList
        }
    ]
})

Mock.onPost("/api/selectTeacher").reply((config:any)=>{
    for(let i = 0; i < teacherList.length; i++){
        if(teacherList[i].id === JSON.parse(config.data).id){
            return [
                200,
                {
                    code:1,
                    msg: "查询成功！",
                    teacherInfo: teacherList[i]
                }
            ]
        }
    }
    return [
        200,
        {
            code: 0,
            msg: "教师不存在！"
        }
    ]
})

Mock.onGet("/api/getStudentList").reply((config:any)=>{
    let stuList:Array<any> = [];
    for(let i = 0; i < teacherList.length; i++ ){
        if(teacherList[i].id === JSON.parse(config.data).id){
            teacherList[i].studentIdList.forEach(s=>{
                stuList.push(getStudentInfo(s));
            })
        }
    }

    function getStudentInfo(stuId: Number){
        for(let i = 0; i < studentList.length; i ++){
            if(studentList[i].id === stuId){
                return studentList[i];
            }
        }
    }
    return [
        200,
        {
            code: 1,
            msg: "查询成功！",
            studentList: stuList
        }
    ]
})
